<?php
/** ***********************
 * 作者：gzy
 * 日期：2018/4/25
 * 作用：发票视图模型
 ** ***********************/
class questionViewHome extends coreFrameworkView
{
	function __construct(){
		parent::__construct();
	}
	//插入log
	function insertApiLog($send_data){
		
		$insert_data = array(
			
		);
		
		$this->model->insertApiLog($insert_data);
		
	}
	//错误代码
	function errorCodeData($error_code){
		$error_msg = array(
			'200'=>'ok',
			'201'=>'error',
			'203'=>'session'
		);
		
		return $error_msg[$error_code];
	}
	//api返回数据处理
	function returnJson($data='',$error_code=200,$msg){
		$send_data = array(
			'error_code'=>$error_code,
			'data'=>$data,
			'error_msg'=>($msg)?$msg:$this->errorCodeData($error_code)
		);
		//$this->insertApiLog($send_data);
		echo json_encode($send_data);
	}
	//添加问题
	function add_question()
	{		
		$post = file_get_contents('php://input');
		$result = (array)json_decode($post);
		$insert_data = array(
			'title'=>$result['title'],
			'question_type'=>$result['question_type'],
			'image'=>$result['image'],
			'content'=>$result['content'],
			'createtime'=>date('Y-m-d H:i:s',time()),
			'province_id'=>$_SESSION["province_id"],
			'city_id'=>$_SESSION["city_id"],
			'country_id'=>$_SESSION["country_id"],
			'shop_id'=>$_SESSION["shop_id"],
			'user_id'=>$_SESSION["user_id"]
		);
		$re = $this->model->add_question($insert_data);
		if($re)	
		{
			$this->returnJson('',200);
		}else{
			$this->returnJson('',201);
		}
	}
	//图片上传接口
	function uploadimage()
	{
		include _SITE_INCLUDE_CLASS_PATH."upfile.php";
		$upfile=new upload();
		$result = $upfile->upImg($_FILES['file']);
		if($result)
		{
			$this->returnJson($result['photo_dir'].$result['photo'],200);
		}else{
			$this->returnJson('',201);
		}
	}
	function tupian()
	{
		$this->tmp->assign('mobile',11);
		$this->createMenu();
		$this->dp('home/com_question/index');
	}
	//问题类型接口
	function category()
	{
		$category = $this->model->selectQeustionCategory();//获取问题类别
		if($category)	
		{
			/*foreach($category as $k=>$v)
			{
				$arr[$v['id']]=$v['name'];
			}*/
			$this->returnJson($category,200);
		}else{
			$this->returnJson('',201);
		}
	}
	//登录接口
	function login()
	{
		$post = file_get_contents('php://input');
		$result = (array)json_decode($post);
		$username=$result['username'];
		$password=$result['password'];
		$result = $this->model->getPersondetail($username,$password);
		if($result)	
		{
			session_start();
			$_SESSION["uid"]=$result[0]['id'];
			$_SESSION["user_id"]=$result[0]['id'];
			$_SESSION["shop_id"]=$result[0]['shop_id'];
			//$fatherid = $this->model->getFatherId($_SESSION["shop_id"]);
			$_SESSION["province_id"]=$result[0]['province_id'];
			$_SESSION["city_id"]=$result[0]['city_id'];
			$_SESSION["country_id"]=$result[0]['country_id'];
			$_SESSION['expiretime']=time()+3600;//设置session过期时间
			$result[0]['expiretime']=$_SESSION['expiretime'];
			$result[0]['session_id']=session_id();
			$this->returnJson($result,200);
		}else{
			$this->returnJson('',201);
		}
	}
	//session登录
	function sessionlogin()
	{
		if($_SESSION["uid"]){
			if($_SESSION['expiretime']<time())
			{
				session_destroy();
				$this->returnJson($_SESSION,203);				
				return false;
			}else{
				$this->returnJson($_SESSION,200);return false;
			}
			
		}else{
			$this->returnJson('',201);
		}
	}
	//退出功能
	function loginout()
	{
		session_destroy();
		$this->returnJson($_SESSION,200);
	}
	//获取问题列表
	function getquestionByUserId()
	{
		$result = $this->model->getquestionByUserId($_SESSION["uid"]);
		if($result)
		{
			$this->returnJson($result,200);
		}else{
			$this->returnJson($result,200);
		}
	}
	function getquestiondetail()
	{
		$post = file_get_contents('php://input');
		$result = (array)json_decode($post);
		$id=$result['id'];
		$re = $this->model->getquestiondetail($id);
		if($re)
		{
			$this->returnJson($re,200);
		}else{
			$this->returnJson($re,200);
		}
	}
}

?>